using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectHoTro()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_HOTRO
			};
			return View(CreateViewModel(data));
		}



        public ActionResult DeleteHoTro(int id, int page = 1)
		{
            var result = _repHoTro.Delete(id);
            var data = new DataTransferViewModel
            {
                InfoText = result ? "Item has been deleted" : "Cannot delete item!",
                EnumViewModelType = EnumViewModel.ADMIN_HOTRO,
                CurrentPage = page
            };
            return View("SelectHoTro", CreateViewModel(data));
		}

		public ActionResult EditHoTro(int page, int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_HOTRO,
				CurrentPage = page,
				IntID = id
			};
			return PartialView("Templates/TH_EditHoTro", CreateViewModel(data));
		}

		public ActionResult CancelEditingHoTro(int page)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_HOTRO,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListHoTro", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateHoTro(FormCollection forms)
		{
			string id = forms.GetValues("HoTro_ID")[0];
			string hoten = forms.GetValues("HoTro_HoTen")[0];
			string nickname = forms.GetValues("HoTro_NickName")[0];
			string phone = forms.GetValues("HoTro_Phone")[0];
			var hotro = _repHoTro.SelectByID(int.Parse(id));
			hotro.HoTen = hoten;
			hotro.NickName = nickname;
			hotro.Phone = phone;
			var result = _repHoTro.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_HOTRO,
			};
            return View("SelectHoTro", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertHoTro(FormCollection forms)
		{
			string hoten = forms.GetValues("HoTro_HoTen")[0];
			string nickname = forms.GetValues("HoTro_NickName")[0];
			string phone = forms.GetValues("HoTro_Phone")[0];
			var newItem = new HoTro
			{
				HoTen = hoten,
				NickName = nickname,
				Phone = phone
			};
			var result = _repHoTro.Insert(newItem);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_HOTRO,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
            return View("SelectHoTro", CreateViewModel(data));
		}


	}
}